﻿Imports Negocio
Imports Utilitario
Public Class frmKardexArticulo

    Dim CIA As String = "01"
    Dim codArticulo As String = ""
    Dim nomArticulo As String = ""
    Dim codProveedor As String = ""
    Dim nomProveedor As String = ""
    Dim nomUnidad As String = ""
    Dim stockSIC As String = ""
    Dim stockSIS As String = ""

    Dim util As New Utiles
    Dim servicio1 As New KardexNegocio

    Sub New(codArt As String, nomArt As String, codPro As String, nomPro As String, nomUni As String, stoSIC As String, stoSIS As String)

        ' Llamada necesaria para el diseñador.
        InitializeComponent()

        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().
        Me.codArticulo = codArt
        Me.nomArticulo = nomArt
        Me.codProveedor = codPro
        Me.nomProveedor = nomPro
        Me.nomUnidad = nomUni
        Me.stockSIC = stoSIC
        Me.stockSIS = stoSIS
    End Sub

    Sub listarMovimientos(codArt As String, codUbi As String, fecha1 As String, fecha2 As String)
        Dim ds As DataSet = servicio1.listarMovimientos(codArt, codUbi, fecha1, fecha2)
        dgvMovimientos.Rows.Clear()

        For i = 0 To ds.Tables(0).Rows.Count - 1
            dgvMovimientos.Rows.Add(ds.Tables(0).Rows(i)("Tipo").ToString.Trim, ds.Tables(0).Rows(i)("Documento").ToString.Trim,
                                    ds.Tables(0).Rows(i)("Fecha").ToString.Trim, ds.Tables(0).Rows(i)("Cantidad").ToString.Trim,
                                    ds.Tables(0).Rows(i)("Usuario").ToString.Trim)
        Next

        If ds.Tables(0).Rows.Count > 0 Then
            dgvMovimientos.Rows(0).Selected = False
        End If
    End Sub

    Sub listarMovimientosXArticulo(codArt As String, fecha1 As String, fecha2 As String)
        Dim ds As DataSet = servicio1.listarMovimientosXArticulo(codArt, fecha1, fecha2)
        dgvUbicaciones.DataSource = ds.Tables(0)
        If ds.Tables(0).Rows.Count > 0 Then
            dgvUbicaciones.Rows(0).Selected = False
        End If
        Cursor.Current = Cursors.Default
    End Sub

    Sub iniciar()
        txtCodArticulo.Text = codArticulo
        txtNomArticulo.Text = nomArticulo
        txtCodProveedor.Text = codProveedor
        txtNomProveedor.Text = nomProveedor
        txtNomUM.Text = nomUnidad
        txtSICOPE.Text = stockSIC
        txtSIS.Text = stockSIS

        listarMovimientosXArticulo(codArticulo, txtFechaInicial.Text.Trim, txtFechaFinal.Text.Trim)
    End Sub

    Private Sub frmKardexArticulo_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        iniciar()
    End Sub

    Private Sub btnActualizar_Click(sender As System.Object, e As System.EventArgs) Handles btnActualizar.Click
        listarMovimientosXArticulo(txtCodArticulo.Text.Trim, txtFechaInicial.Text.Trim, txtFechaFinal.Text.Trim)
        dgvMovimientos.Rows.Clear()
    End Sub

    Private Sub txtCodArticulo_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtCodArticulo.KeyDown
        Dim texto As String = txtCodArticulo.Text.Trim
        If e.KeyValue = Keys.Enter Then
            Cursor.Current = Cursors.WaitCursor
            If texto = "" Then
                txtCodArticulo.Text = ""
                txtNomArticulo.Text = ""
                txtCodProveedor.Text = ""
                txtNomProveedor.Text = ""
                txtNomUM.Text = ""
                txtSICOPE.Text = ""
                txtSIS.Text = ""

                listarMovimientosXArticulo(texto, txtFechaInicial.Text.Trim, txtFechaFinal.Text.Trim)
            Else
                Dim ds As DataSet = servicio1.listadoStockArticulos(CIA, texto)

                If ds.Tables(0).Rows.Count = 0 Then
                    util.mensaje("No hay registros por mostrar o Artículo no existe.")
                    txtCodArticulo.Text = ""
                    Exit Sub
                    Cursor.Current = Cursors.Default
                End If

                txtCodArticulo.Text = ds.Tables(0).Rows(0)("ART_Codigo").ToString.Trim
                txtNomArticulo.Text = ds.Tables(0).Rows(0)("Articulo").ToString.Trim
                txtCodProveedor.Text = ds.Tables(0).Rows(0)("PROV_Codigo").ToString.Trim
                txtNomProveedor.Text = ds.Tables(0).Rows(0)("Proveedor").ToString.Trim
                txtNomUM.Text = ds.Tables(0).Rows(0)("UNM_Abreviatura").ToString.Trim
                txtSICOPE.Text = ds.Tables(0).Rows(0)("SICOPE").ToString.Trim
                txtSIS.Text = ds.Tables(0).Rows(0)("SIS").ToString.Trim

                listarMovimientosXArticulo(texto, txtFechaInicial.Text.Trim, txtFechaFinal.Text.Trim)
            End If
        End If
    End Sub

    Private Sub dgvUbicaciones_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvUbicaciones.CellClick
        Dim f As Integer = e.RowIndex

        If f > -1 Then
            listarMovimientos(txtCodArticulo.Text.Trim, dgvUbicaciones.Item("Column2", f).Value, txtFechaInicial.Text, txtFechaFinal.Text)
        End If
    End Sub

End Class